package com.biaobiaoqi.scoreanalysis.serviceImpl;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import com.biaobiaoqi.scoreanalysis.exception.DuplicateTeacherException;
import com.biaobiaoqi.scoreanalysis.exception.NoSuchTeacherFoundException;
import com.biaobiaoqi.scoreanalysis.hibernate.bean.Teacher;
import com.biaobiaoqi.scoreanalysis.service.ITeacherService;
import com.biaobiaoqi.scoreanalysis.util.Gender;

public class TeacherServiceImpl extends DaoServiceImpl<Teacher> implements
		ITeacherService {

	@Override
	public void addTeacher(String loginName, String password, String gender,
			String birthday, String phone, String email) throws DuplicateTeacherException, ParseException {
		Gender aGender;
        java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd");
        Date birthday2 = sdf.parse(birthday);
		if (gender.equals("male")) {
			aGender = Gender.MALE;
		} else {
			aGender = Gender.FEMALE;
		}
		if (isLoginNameAvailable(loginName)) {
			Teacher teacher = new Teacher();
			teacher.setLoginName(loginName);
			teacher.setPassword(password);
			teacher.setGender(aGender);
			teacher.setBirthday(birthday2);
			teacher.setPhoneNumber(phone);
			teacher.setEmail(email);
			getDao().create(teacher);
		} else {
			throw new DuplicateTeacherException();
		}

	}

	@Override
	public void deleteTeacher(int teacherId) throws NoSuchTeacherFoundException {

		Teacher teacher = findTeacher(teacherId);
		if (teacher == null) {
			throw new NoSuchTeacherFoundException();
		} else {
			getDao().delete(teacher);
		}
	}

	@Override
	public void deleteTeacher(String loginName)
			throws NoSuchTeacherFoundException {
		Teacher teacher = findTeacher(loginName);
		if (teacher == null) {
			throw new NoSuchTeacherFoundException();
		} else {
			getDao().delete(teacher);
		}
	}

	@Override
	public void modifyTeacher(int teacherId, Gender gender, Date birthday,
			String phone) throws NoSuchTeacherFoundException {
		Teacher teacher = findTeacher(teacherId);
		if (teacher == null) {
			throw new NoSuchTeacherFoundException();
		} else {
			teacher.setGender(gender);
			teacher.setBirthday(birthday);
			teacher.setPhoneNumber(phone);
			getDao().save(teacher);
		}
	}

	@Override
	public Teacher findTeacher(String loginName) {
		return getDao().queryUnique(
				"select t from Teacher t where t.loginName=?", loginName);
	}

	@Override
	public Teacher findTeacher(int teacherId) {
		return getDao().findById(Teacher.class, teacherId);
	}

	@Override
	public List<Teacher> listAllTeacher() {
		return getDao().queryList("from Teacher t");
	}

	@Override
	public List<Teacher> listAllTeacher(int from, int number) {
		return getDao().queryList("from Teacher t", from, number);
	}

	@Override
	public boolean isLoginNameAvailable(String loginName) {
		Teacher teacher = findTeacher(loginName);
		if (teacher == null) {
			return true;
		}
		return false;
	}

}
